import random

goods = [
    {"name": "电脑", "price": 5000},
    {"name": "手机", "price": 3000},
    {"name": "平板", "price": 2000},
    {"name": "鼠标", "price": 50},
    {"name": "键盘", "price": 100},
]  # 随便定义几个商品

min_price = min(good["price"] for good in goods)

money = 10000  # 初始化余额
shopping_cart = []  # 初始化购物车为空列表

discount_rate = 1.0  # 定义初始折扣率，默认为不打折

coupons = ["8折券", "7折券", "6折券", "5折券"]  # 给几个优惠情况

coupon = random.choice(coupons)  # 随机选择一张优惠券

print("欢迎光临本商城！您的优惠券是：{}".format(coupon))

if coupon == "8折券":
    discount_rate = 0.8
elif coupon == "7折券":
    discount_rate = 0.7
elif coupon == "6折券":
    discount_rate = 0.6
elif coupon == "5折券":
    discount_rate = 0.5

while money >= min_price:
    print("商城有以下商品：")
    for i, good in enumerate(goods):
        print("{}、{}（价格：{}元）".format(i+1, good["name"], good["price"]))

    choice = input("请输入想要购买的商品编号（Q表示退出）：")

    if choice.upper() == "Q":
        break  # 输入 Q 表示结束购物

    if not choice.isdigit() or int(choice) < 1 or int(choice) > len(goods):
        print("对不起，您输入的商品编号不存在，请重新输入！")
        continue  # 如果输入非法，则重新输入

    index = int(choice) - 1
    selected_good = goods[index]

    if money >= selected_good["price"] * discount_rate:
        shopping_cart.append(selected_good)
        money -= selected_good["price"] * discount_rate
        print("添加成功！")
    else:
        print("对不起，您的余额不足，请到其他商场购买！")
        continue

print("您本次购物的明细如下：")
total_price = 0
for i, good in enumerate(shopping_cart):
    print("{}、{}（价格：{}元）".format(i+1, good["name"], good["price"] * discount_rate))
    total_price += good["price"] * discount_rate

print("购物时间：{}".format("2023年4月19日"))
print("卡余额：{}元".format(money))
print("购物数量：{}件".format(len(shopping_cart)))
print("购买总商品的价值：{}元".format(total_price))

print("欢迎下次光临！")